package com.cloud.user.controller;

import com.cloud.user.entity.AjaxJson;
import com.cloud.user.entity.User;
import com.cloud.user.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by Administrator on 2018/1/7.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = GET)
    public String index(Model model) {
        return "login";
    }

    @ApiOperation(value="检查用户", notes="根据用户名检查该用户是否存在")// 使用该注解描述接口方法信息
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名称", required = true, dataType = "String", paramType="path")
    })// 使用该注解描述方法参数信息，此处需要注意的是paramType参数，需要配置成path，否则在UI中访问接口方法时，会报错
    @RequestMapping(value = "/exist/{username}",method = GET)
    @ResponseBody
    public AjaxJson checkUser(@PathVariable("username")String username){
        AjaxJson jsonReturn = new AjaxJson();
        User result = userService.checkUserExits(username);
        if (result != null) {
            jsonReturn.setSuccess(true);
            jsonReturn.setObj(result);
        } else {
            jsonReturn.setSuccess(false);
        }
        return jsonReturn;
    }
}
